package com.taotao.rop.service;

import com.rop.annotation.HttpAction;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.taotao.rop.database.domain.PlayGame;
import com.taotao.rop.database.mapper.PlayGameMapper;
import com.taotao.rop.response.BaseResponse;
import com.taotao.rop.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@ServiceMethodBean(httpAction = HttpAction.POST, version = "1.0")
public class PlayGameService {
    @Autowired
    private PlayGameMapper playGameMapper;

    @ServiceMethod(method = "playGame.insert", needInSession = NeedInSessionType.NO)
    public Object insertPlayGame(PlayGame request) {
        Date now = new Date();
        request.setStartTime(DateTimeUtil.getDateStartMillis(now));
        request.setEndTime(DateTimeUtil.getDateEndMillis(now));
        List<PlayGame> playGameList = playGameMapper.getNotEndListToday(request);
        Long createTime = DateTimeUtil.getDateMillis(new Date());
        Integer count;
        if (playGameList != null && playGameList.size() > 0) {
            PlayGame playGame = new PlayGame();
            playGame.setId(playGameList.get(0).getId());
            playGame.setCreateTime(createTime);
            count = playGameMapper.updateOne(playGame);
        } else {
            request.setCreateTime(createTime);
            count = playGameMapper.insertOne(request);
        }

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }

    @ServiceMethod(method = "playGame.getNameToday", needInSession = NeedInSessionType.NO)
    public Object getInputGameNameToday(PlayGame request) {
        PlayGame condition = new PlayGame();
        condition.setAppId(request.getAppId());
        condition.setOpenId(request.getOpenId());
        Date now = new Date();
        condition.setStartTime(DateTimeUtil.getDateStartMillis(now));
        condition.setEndTime(DateTimeUtil.getDateEndMillis(now));

        List<String> gameNameList = playGameMapper.getInputGameNameToday(condition);
        BaseResponse<List<String>> response = new BaseResponse<List<String>>();
        response.setSuccess(true);
        response.setData(gameNameList);
        return response;
    }

    @ServiceMethod(method = "playGame.getLatestInputTelToday", needInSession = NeedInSessionType.NO)
    public Object getLatestInputTelToday(PlayGame request) {
        PlayGame condition = new PlayGame();
        condition.setAppId(request.getAppId());
        condition.setOpenId(request.getOpenId());
        condition.setGameName(request.getGameName());
        Date now = new Date();
        condition.setStartTime(DateTimeUtil.getDateStartMillis(now));
        condition.setEndTime(DateTimeUtil.getDateEndMillis(now));

        List<String> gameNameList = playGameMapper.getLatestInputTelToday(condition);
        BaseResponse<List<String>> response = new BaseResponse<List<String>>();
        response.setSuccess(true);
        response.setData(gameNameList);
        return response;
    }

    /**当天*/
    @ServiceMethod(method = "playGame.getJoinCountToday", needInSession = NeedInSessionType.NO)
    public Object getJoinCountToday(PlayGame request) {
        Date now = new Date();
        request.setStartTime(DateTimeUtil.getDateStartMillis(now));
        request.setEndTime(DateTimeUtil.getDateEndMillis(now));

        int count = playGameMapper.getJoinCountToday(request);
        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }

    /**当天*/
    @ServiceMethod(method = "playGame.getTelJoinCountToday", needInSession = NeedInSessionType.NO)
    public Object getTelJoinCountToday(PlayGame request) {
        Date now = new Date();
        request.setStartTime(DateTimeUtil.getDateStartMillis(now));
        request.setEndTime(DateTimeUtil.getDateEndMillis(now));

        int count = playGameMapper.getTelJoinCountToday(request);
        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }

    @ServiceMethod(method = "playGame.list", needInSession = NeedInSessionType.NO)
    public Object getList(PlayGame request) {
        List<PlayGame> playGameList = playGameMapper.getList(request);

        BaseResponse<List<PlayGame>> response = new BaseResponse<List<PlayGame>>();
        response.setSuccess(true);
        response.setData(playGameList);
        return response;
    }

    @ServiceMethod(method = "playGame.listByPage", needInSession = NeedInSessionType.NO)
    public Object getListByPage(PlayGame request) {
        List<PlayGame> playGameList = playGameMapper.getListByPage(request);

        BaseResponse<List<PlayGame>> response = new BaseResponse<List<PlayGame>>();
        response.setSuccess(true);
        response.setMsg("" + playGameMapper.getCount(request));
        response.setData(playGameList);
        return response;
    }

    @ServiceMethod(method = "playGame.end", needInSession = NeedInSessionType.NO)
    public Object endGame(PlayGame request) {
        int count = playGameMapper.endGame(request);

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }

    @ServiceMethod(method = "playGame.top50", needInSession = NeedInSessionType.NO)
    public Object getTop50(PlayGame request) {
        BaseResponse<List<PlayGame>> response = new BaseResponse<List<PlayGame>>();
        response.setSuccess(true);
        response.setData(playGameMapper.getTop50Prize(request.getGameName()));
        return response;
    }

    @ServiceMethod(method = "playGame.getMyPrizes", needInSession = NeedInSessionType.NO)
    public Object getMyPrizes(PlayGame request) {
        BaseResponse<List<PlayGame>> response = new BaseResponse<List<PlayGame>>();
        response.setSuccess(true);
        response.setData(playGameMapper.getMyPrizes(request));
        return response;
    }

    @ServiceMethod(method = "playGame.getLatestInputGameName", needInSession = NeedInSessionType.NO)
    public Object getLatestInputGameName(PlayGame request) {
        BaseResponse<String> response = new BaseResponse<String>();
        response.setSuccess(true);
        response.setData(playGameMapper.getLatestInputGameName(request));
        return response;
    }

    @ServiceMethod(method = "playGame.updateGame", needInSession = NeedInSessionType.NO)
    public Object updateGame(PlayGame request) {
        int count = playGameMapper.updateGame(request);

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }

    @ServiceMethod(method = "playGame.updateGamePrize", needInSession = NeedInSessionType.NO)
    public Object updateGamePrize(PlayGame request) {
        int count = playGameMapper.updateGamePrize(request);

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }
}
